<?php
/* @var $this ZoneController */
/* @var $model Zone */
/* @var horaires array */
/* @var $form CActiveForm */
/* @var $enable_add_more_horaires bool */
?>
<?php
//Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl . '/css/form.css')
?>
<div class="form">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
	  'id' => 'zone-form',
	  'enableClientValidation' => true,
	  'enableAjaxValidation' => true,
	  'htmlOptions' => array(
		'class' => 'my-form-class',
	  ),
    ));
    ?>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
	  <?php echo $form->labelEx($model, 'modeid'); ?>
	  <?php
	  $this->widget('ModeButtons', array(
		'model' => $model,
	  ));
	  ?>

	  <?php echo $form->error($model, 'modeid'); ?>
    </div>
    <div class="row">
	  <?php echo $form->labelEx($model, 'jusqua'); ?>
	  <?php
	  /*
	    Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
	    $this->widget('CJuiDateTimePicker', array(
	    'model' => $model, //Model object
	    'attribute' => 'jusqua', //attribute name
	    'mode' => 'datetime', //use "time","date" or "datetime" (default)
	    'options' => array() // jquery plugin options
	    ));
	   */
	  echo $form->textField($model, 'jusqua');
	  ?>
	  <?php echo $form->error($model, 'jusqua'); ?>
    </div>
    <?php
    //$indexes = array();
    $index = 0;
    foreach (Yii::app()->controller->module->periodes as $periodeid => $periode) {
	  ?>
        <fieldset>
    	  <legend>Horaires <?php echo $periode; ?></legend>

    	  <div class=horaires" id="horaires_<?php echo $periodeid; ?>">
		    <?php
		    //$index = 0;

		    foreach ($model->horaires as $index => $horaire):
			  if ($horaire->periodeid == $periodeid) {
				$this->renderPartial('/horaire/_form', array(
				    'model' => $horaire,
				    'index' => $index,
				    'display' => 'block',
				    'periodeid' => $periodeid,
				));
				//$index++;
			  }
		    endforeach;
		    $index++;
		    //$indexes[$periodeid] = $index;
		    ?>
    	  </div><div style="clear:both;"></div>
		<?php
		echo CHtml::link('Ajouter un horaire', '#', array('title' => 'Ajouter un horaire', 'class' => 'loadHoraireByAjax add', 'id' => 'loadHoraireByAjax_' . $periodeid));
		?>
        </fieldset>
	  <?php
    }

    // Creation du tableau associatif (objet) des index en js
    //array_walk($indexes, create_function('&$i,$k', '$i="\"$k\" : $i";'));
    // $indexes = '{' . implode($indexes, ", ") . '}';
    ?>


    <div class="row">
	  <?php echo $form->labelEx($model, 'nom'); ?>
	  <?php echo $form->textField($model, 'nom', array('size' => 45, 'maxlength' => 45)); ?>
	  <?php echo $form->error($model, 'nom'); ?>
    </div>


    <div class="row buttons">
	  <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

    <?php $this->endWidget(); ?>

</div><!-- form -->

<?php

//$this->widget('application.components.jqueryWidget');
Yii::app()->domoHelper->loadJquery();


Yii::app()->clientScript->registerScript('loadchild', '
var _index =  ' . $index . ';
$(".loadHoraireByAjax").click(function(e){
    e.preventDefault();
    var _periodeid = this.id.replace("loadHoraireByAjax_", "");
   var _url = "' . Yii::app()->controller->createUrl("loadHoraireByAjax", array("load_for" => $this->action->id)) . '?index="+_index;
     $.ajax({
        url: _url,
        success:function(response){
            $("#horaires_"+_periodeid).append(response);
            $("#horaires_"+_periodeid+" .crow").last().animate({
                opacity : 1, 
                left: "+50", 
                height: "toggle"
            });
        }
    });
    _index++;
});
', CClientScript::POS_END);

Yii::app()->clientScript->registerScript('deleteChild', "
function deleteChild(elm, index)
{
    element=$(elm).parent().parent();
    /* animate div */
    $(element).animate(
    {
        opacity: 0.25, 
        left: '+=50', 
        height: 'toggle'
    }, 500,
    function() {
        /* remove div */
        $(element).remove();
    });
}", CClientScript::POS_END);

/* Yii::app()->clientScript->registerScript('fieldsetFold', '
  $(function(){
  $("legend").click(function(){
  //$(this).parent().find(".horaires").slideToggle("slow");
  $(this).siblings().slideToggle("slow");
  });
  });
  ', CClientScript::POS_END); */
?>